home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / motif / oclip.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  8KB  |  291 lines

  1. /*
  2.  * Copyright (c) 1993-94, Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  21.  */
  22. /*----------------------------------------------------------------------------
  23.  *
  24.  * oclip.c : openGL (motif) example showing how to use arbitrary clipping plane.
  25.  *
  26.  * Author : Yusuf Attarwala
  27.  *          SGI - Applications
  28.  * Date   : Mar 93
  29.  *
  30.  *    note : the main intent of this program is to demo the arbitrary
  31.  *           clipping functionality, hence the rendering is kept
  32.  *           simple (wireframe) and only one clipping plane is used.
  33.  *
  34.  *    press  left   button to move object 
  35.  *           right  button to move clipping plane
  36.  *
  37.  *
  38.  *---------------------------------------------------------------------------*/
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41.  
  42. #include <Xm/Xm.h> 
  43. #include <Xm/Frame.h>               /* for frame widgets */
  44. #include <Xm/Form.h>               /* for frame widgets */
  45. #include <X11/keysym.h>             /* keyboard translations */
  46. #include <X11/StringDefs.h>
  47.  
  48. #include <GL/gl.h>                  /* gl includes */
  49. #include <GL/glu.h>                 /* utility library includes */
  50. #include <GL/GLwMDrawA.h>           /* include for the drawing area widget */
  51.  
  52. /* function declarations */
  53.  
  54. void 
  55.     createToplevel(void),
  56.     drawScene(void),
  57.     setMatrix(void),
  58.     animateClipPlane(void),
  59.     animation(void),
  60.     exposeCB(Widget,XtPointer,XtPointer),
  61.     resizeCB(Widget,XtPointer,XtPointer),
  62.     initCB(Widget,XtPointer,XtPointer),
  63.     inputCB(Widget,XtPointer,XtPointer);
  64.  
  65.  
  66. /* global variables */
  67.             
  68. Display       *display;             /* current display */
  69. XtAppContext  appContext;           /* X application context */
  70. float         ax,ay,az;             /* angles for animation */
  71. GLUquadricObj *quadObj;             /* used in drawscene */
  72. GLdouble planeEqn[] = {0.707,0.707,0.0,0.0};   /* initial clipping plane eqn */
  73.  
  74. Widget        toplevel,       /* toplevel shell */
  75.               glw;            /* current widget */
  76.              
  77. GLXContext    glxc;           /* current glx context */
  78.  
  79. Arg args[20];
  80. int acnt;
  81.  
  82. void 
  83. main(int argc, char** argv)
  84. {
  85.     XtToolkitInitialize();
  86.     appContext = XtCreateApplicationContext();
  87.     display    = XtOpenDisplay(appContext, NULL, "Oclip","oclip",NULL,0,
  88.                               &argc,argv);
  89.     if (!display) {
  90.         printf("%s : Unable to open display\n",argv[0]);
  91.         exit(0);
  92.     }
  93.  
  94.     printf("\n---------------------------------------------\n");
  95.     printf("OpenGL arbitrary clipping plane example \n\n");
  96.     printf("Press:  left   button to move object\n\
  97.         right  button to move clipping plane\n");
  98.  
  99.     quadObj = gluNewQuadric ();   /* this will be used in drawScene */
  100.     createToplevel();             /* create widget hierarchy */
  101.     XtAppMainLoop(appContext);    /* loop forever */
  102. }
  103.  
  104.  
  105. void
  106. createToplevel(void)
  107. {
  108.     Widget frame;
  109.  
  110.     acnt = 0;
  111.     XtSetArg(args[acnt],XmNminHeight, 300);acnt++;
  112.     XtSetArg(args[acnt],XmNminWidth,  300);acnt++;
  113.     XtSetArg(args[acnt],XmNminAspectX,  1);acnt++;
  114.     XtSetArg(args[acnt],XmNminAspectY,  1);acnt++;
  115.     XtSetArg(args[acnt],XmNmaxAspectX,  1);acnt++;
  116.     XtSetArg(args[acnt],XmNmaxAspectY,  1);acnt++;
  117.     toplevel  = XtAppCreateShell("openGL clip","xtext",
  118.                                   applicationShellWidgetClass,
  119.                                   display,args,acnt);
  120.  
  121.     /* create a frame to hold glx widget */
  122.     frame   = XtVaCreateManagedWidget("frame", 
  123.                   xmFrameWidgetClass, toplevel, 
  124.                   NULL);
  125.  
  126.     /* create a double buffer widget, in rgb mode and manage it */
  127.     acnt = 0;
  128.     XtSetArg(args[acnt], GLwNrgba,               TRUE); acnt++;
  129.     XtSetArg(args[acnt], GLwNdoublebuffer,       TRUE); acnt++;
  130.     XtSetArg(args[acnt], GLwNallocateBackground, TRUE); acnt++;
  131.     glw = GLwCreateMDrawingArea(frame, "glw", args, acnt);
  132.     XtManageChild(glw);
  133.  
  134.     /* register callbacks */
  135.     XtAddCallback(glw, GLwNginitCallback,  initCB,   NULL);
  136.  
  137.     /* realize widget hierarchy */
  138.     XtRealizeWidget(toplevel);
  139.  
  140.     /* additional callbacks */
  141.     XtAddCallback(glw, GLwNexposeCallback, exposeCB, NULL);
  142.     XtAddCallback(glw, GLwNresizeCallback, resizeCB, NULL);
  143.     XtAddCallback(glw, GLwNinputCallback,  inputCB,  NULL);
  144.  
  145. }
  146.  
  147. void
  148. drawScene(void)
  149. {
  150.     glClearColor(0.0, 0.0, 0.0, 0.0);
  151.     glClear(GL_COLOR_BUFFER_BIT);
  152.  
  153.     glPushMatrix();
  154.     gluQuadricDrawStyle (quadObj, GLU_LINE);
  155.     glColor3f (1.0, 1.0, 0.0);
  156.     glRotatef (ax,1.0,0.0,0.0);
  157.     glRotatef (-ay,0.0, 1.0, 0.0);
  158.  
  159.     glClipPlane(GL_CLIP_PLANE0,planeEqn);      /* define clipping plane */
  160.     glEnable(GL_CLIP_PLANE0);                  /* and enable it */
  161.  
  162.     gluCylinder (quadObj, 2.0,5.0,10.0,20,8);  /* draw a cone */
  163.  
  164.     glDisable(GL_CLIP_PLANE0);
  165.     glPopMatrix();
  166.  
  167.     glFlush();
  168.     glXSwapBuffers(XtDisplay(glw), XtWindow(glw));
  169.  
  170. }
  171.  
  172. void
  173. setMatrix(void)
  174. {
  175.     glMatrixMode(GL_PROJECTION);
  176.     glLoadIdentity();
  177.     glOrtho(-15.0,15.0,-15.0,15.0,-10.0,10.0);
  178.     glMatrixMode(GL_MODELVIEW);
  179.     glLoadIdentity();
  180. }
  181.  
  182. void
  183. animation(void)
  184. {
  185.     register int i;
  186.     /* animate the cone */
  187.  
  188.     for (i=0;i<60;i++) {
  189.         ax += 5.0;
  190.         ay -= 2.0;
  191.         az += 5.0;
  192.         if (ax >= 360)  ax = 0.0;
  193.         if (ay <= -360) ay = 0.0;
  194.         if (az >= 360)  az = 0.0;
  195.         drawScene();
  196.     }
  197. }
  198.  
  199. void
  200. animateClipPlane(void)
  201. {
  202.     register int i;
  203.     static int sign = 1;
  204.  
  205.     for (i=0;i<5;i++) {
  206.     planeEqn[3] += sign*0.5;
  207.     if (planeEqn[3] > 4.0) sign = -1;
  208.     else if (planeEqn[3] < -4.0) sign = 1;
  209.         drawScene();
  210.     }
  211. }
  212.  
  213. void 
  214. inputCB(Widget w, XtPointer client_data, XtPointer call)
  215. {
  216.     char            string[31];
  217.     XComposeStatus  composeStatus;
  218.     KeySym keysym;
  219.     GLwDrawingAreaCallbackStruct *call_data;
  220.  
  221.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  222.  
  223.     switch(call_data->event->type) {
  224.     case ButtonPress:
  225.         switch (call_data->event->xbutton.button) {
  226.         case Button1:
  227.         animation();
  228.             break;
  229.         case Button2 :
  230.         printf("I (button2) don't do anything\n");
  231.             break;
  232.         case Button3 :
  233.         animateClipPlane();
  234.             break;
  235.         }
  236.         break;
  237.     case KeyPress :
  238.         XLookupString(&call_data->event->xkey,string,
  239.                       30,&keysym,&composeStatus);
  240.         switch(keysym) {
  241.         case XK_Escape :
  242.             exit(0);
  243.             break;
  244.         }
  245.     break;
  246.     default:
  247.         break;
  248.     }
  249. }
  250.  
  251. void 
  252. resizeCB(Widget w, XtPointer client_data, XtPointer call)
  253. {
  254.     GLwDrawingAreaCallbackStruct *call_data;
  255.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  256.  
  257.  
  258.     GLwDrawingAreaMakeCurrent(w, glxc);
  259.     glViewport(0, 0, call_data->width, call_data->height);
  260.     setMatrix();
  261.     drawScene();
  262. }
  263.  
  264. void 
  265. exposeCB(Widget w, XtPointer client_data, XtPointer call)
  266. {
  267.     GLwDrawingAreaCallbackStruct *call_data;
  268.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  269.  
  270.  
  271.     GLwDrawingAreaMakeCurrent(w, glxc);
  272.     glViewport(0, 0, call_data->width, call_data->height);
  273.     drawScene();
  274. }
  275.  
  276. void
  277. initCB(Widget w, XtPointer client_data, XtPointer call)
  278. {
  279.     XVisualInfo *vi;
  280.  
  281.  
  282.     XtSetArg(args[0], GLwNvisualInfo, &vi);
  283.     XtGetValues(w, args, 1);
  284.  
  285.     glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
  286.  
  287.     ax = 10.0;
  288.     ay = -10.0;
  289.     az = 0.0;
  290. }
  291.